package com.sukaiyi.giteeideaplugin.mainwindow;

import com.intellij.openapi.editor.Document;
import com.intellij.openapi.editor.EditorFactory;
import com.intellij.openapi.ui.DialogWrapper;
import com.intellij.ui.EditorTextField;
import com.intellij.util.ui.JBUI;
import com.sukaiyi.giteeideaplugin.model.CodeSegmentNode;
import com.sukaiyi.giteeideaplugin.utils.FileTypeUtils;
import com.sukaiyi.giteeideaplugin.utils.IdeaUIUtils;
import com.sukaiyi.giteeideaplugin.utils.NotificationUtils;
import com.sukaiyi.giteeideaplugin.utils.ProjectUtils;
import com.sukaiyi.giteejavaapi.common.Gitee;
import com.sukaiyi.giteejavaapi.common.Params;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import javax.swing.*;
import javax.swing.border.Border;
import java.awt.*;
import java.util.HashMap;
import java.util.Map;

public class CodeSegmentWindowDialogWrapper extends DialogWrapper {

    private final CodeSegmentNode node;
    private EditorTextField codeTextField;

    public CodeSegmentWindowDialogWrapper(CodeSegmentNode node) {
        super(true);
        this.node = node;
        setTitle(node.getName());
        init();
        setModal(true);
    }

    @Nullable
    @Override
    protected JComponent createCenterPanel() {
        String content = node.getContent();
        codeTextField = IdeaUIUtils.createEditorTextField(ProjectUtils.getProject(), FileTypeUtils.getFileTypeForName(node.getName()));
        codeTextField.setPreferredSize(new Dimension(700, 400));
        Document document = EditorFactory.getInstance().createDocument(content.replace("\r\n", "\n"));
        codeTextField.setDocument(document);
        codeTextField.setCaretPosition(0);
        codeTextField.setBorder(BorderFactory.createEmptyBorder());
        return codeTextField;
    }

    @Nullable
    @Override
    protected Border createContentPaneBorder() {
        return JBUI.Borders.empty();
    }

    @Nullable
    @Override
    protected String getDimensionServiceKey() {
        return this.getClass().getName();
    }

    @NotNull
    @Override
    protected Action[] createActions() {
        return super.createActions();
    }

    @Override
    public void doCancelAction() {
        super.doCancelAction();
    }

    @Override
    protected void doOKAction() {
        String content = codeTextField.getText();
        Map<String, Map<String, String>> filesParam = new HashMap<>();
        filesParam.put(node.getName(), new HashMap<String, String>() {{
            put("content", content);
        }});
        Params params = Params.create()
                .set("id", node.getId())
                .set("files", filesParam);
        Gitee.getInstance().invoke("patchV5GistsId", params)
                .whenComplete((httpResponse, throwable) -> {
                    if (throwable != null) {
                        NotificationUtils.notify(throwable.getMessage());
                    } else {
                        node.setContent(content);
                    }
                    SwingUtilities.invokeLater(() -> close(OK_EXIT_CODE));
                });
    }
}
